/**
 * Project: topdt-jbpm
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.form;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.topdt.jbpm.Constants;
import com.topdt.jbpm.model.FormInstance;

/**
 * 建立日期 : 2013-5-20 上午11:47:14<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class FormUI {

	private UIType uiType;// ui类型

	private String uiURL;// ui的URL

	private byte[] uiByte;//

	private String uiText;

	private Map fieldControl;
	private Map funcControl;
	private Document doc;

	public enum UIType {
		textXml, textJson, textHtml, url, iByte
	}

	private FormUI() {

	}

	public FormUI(Map fieldControl, Map funcControl, Document doc) {
		this.fieldControl = fieldControl;
		this.funcControl = funcControl;
		this.doc = doc;
	}

	public void response(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		switch (this.uiType) {

		case textXml:
			response.setContentType(Constants.CONTENT_TYPE_XML);
			response.getWriter().write(this.uiText);
			break;
		case textJson:
			response.setContentType(Constants.CONTENT_TYPE_JSON);
			response.getWriter().write(this.uiText);
			break;
		case textHtml:
			response.setContentType(Constants.CONTENT_TYPE_HTML);
			response.getWriter().write(this.uiText);
			break;
		case url:
			/*request.setAttribute("fieldControl", fieldControl);
			request.setAttribute("funcControl", funcControl);*/
			if(doc!=null){
				Map<String, Object> value = new HashMap<String, Object>();
				FormInstance formInstance = (FormInstance) doc;
				if (formInstance.getFormData()!= null) {
					value = JSON.parseObject(new String(formInstance.getFormData(),"utf-8"));
					for(String key:value.keySet()){
						request.setAttribute(key, value.get(key));
					}
				String dd =	(String) request.getAttribute("leaveType");
				System.out.println(dd);
				}
			}
			RequestDispatcher dispatcher = request.getRequestDispatcher(this.uiURL);
			dispatcher.forward(request, response);
			break;
		case iByte:
			response.setContentType(Constants.CONTENT_TYPE_HTML);
			response.getOutputStream().write(this.uiByte);
			break;
		default:
			response.setContentType(Constants.CONTENT_TYPE_TEXT);
			response.getWriter().print("业务表单不正确！");
		}

	}

	public UIType getUiType() {
		return uiType;
	}

	public void setUiType(UIType uiType) {
		this.uiType = uiType;
	}

	public String getUiURL() {
		return uiURL;
	}

	public void setUiURL(String uiURL) {
		this.uiURL = uiURL;
	}

	public byte[] getUiByte() {
		return uiByte;
	}

	public void setUiByte(byte[] uiByte) {
		this.uiByte = uiByte;
	}

	public String getUiText() {
		return uiText;
	}

	public void setUiText(String uiText) {
		this.uiText = uiText;
	}

	public Map getFieldControl() {
		return fieldControl;
	}

	public void setFieldControl(Map fieldControl) {
		this.fieldControl = fieldControl;
	}

	public Map getFuncControl() {
		return funcControl;
	}

	public void setFuncControl(Map funcControl) {
		this.funcControl = funcControl;
	}

	public Document getDoc() {
		return doc;
	}

	public void setDoc(Document doc) {
		this.doc = doc;
	}

}
